package cn.finedo.controller;

import cn.finedo.dto.CustomException;
import cn.finedo.dto.LLMRequestDto;
import cn.finedo.dto.LLMResDto;
import cn.finedo.service.AILLMService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;

@Controller
@RequestMapping("/aillm")
@Scope("singleton")
public class AILLMController {

    @Autowired
    private AILLMService aillmService;

    /**
     * 模型调用
     * @param dto
     * @return
     */
    @PostMapping(value = "/stream", produces = "text/event-stream")
    public StreamingResponseBody streamSseEvents(@RequestBody LLMRequestDto dto) {
        return outputStream -> aillmService.streamSseEvent(dto, outputStream);
    }

    @ResponseBody
    @RequestMapping({"/nonstream"})
    public LLMResDto sendEvents(@RequestBody LLMRequestDto dto) throws CustomException {
        return aillmService.sendEvent(dto);
    }

    @ResponseBody
    @RequestMapping({"/summary"})
    public JSONObject summaryEvents(@RequestBody LLMRequestDto dto) {
        return aillmService.summaryReceipt(dto);
    }

}
